import { formatAddress } from 'pub-bbx-utils';
/** 将form对象转成产品注册信息对象，用于提交表单 */
export function packToProductRegister(fields, form){
  let productRegister = {};
  let attribute = {};
  const {type, serialNumber, name, id } = form;
  let tv = null;

  let registerValue

  fields.forEach(f => {
    if (!registerValue && f.isRegister) {
      registerValue = form[f.fieldName]
    }
    if (!f.isSystem) {
      attribute[f.fieldName] = form[f.fieldName];
    } else {
      productRegister[f.fieldName] = form[f.fieldName];
    }
    
    if (f.formType === 'address' && !f.isSystem) {
      tv = form[f.fieldName];

      let all = formatAddress(tv,'');
      
      attribute[f.fieldName] = {
        ...tv,
      };
      
      all ? attribute[f.fieldName]['all'] = all : '';
    }
    
    if ((f.formType === 'location') && !attribute[f.fieldName].isHaveLocation) {
      attribute[f.fieldName] = {};
    }
    
  });
  
  if (id) {
    productRegister.id = id;
  }
  
  return {
    ...productRegister,
    name,
    type,
    serialNumber,
    attribute,
    catalogId: form.catalogId,
    registerValue
  };
}


/** 将产品注册信息对象转成form表单，用于初始化表单，暂时没用到 */
export function packToForm(field, product, isEdit){
  const {id, name, serialNumber, type } = product;
  let form = {};
  
  return {
    id,
    name,
    serialNumber,
    type,
    ...form,
    ...product.attribute,
  }
}
